feat(data-structure): JS learning collection — 11 structures + tests#13
Open
feat(data-structure): JS learning collection — 11 structures + tests#13
Conversation
- Add Queue class with FIFO functionality backed by a linked list. - Add Stack class with LIFO functionality backed by a JavaScript array. - Add Trie class for efficient prefix-based word storage and retrieval. - Add UnionFind class for disjoint set operations with path compression and union by rank. - Add various data structure tests for validation and correctness. - Create README.md for documentation on data structures and their use cases.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Builds out
data-structure/into a teaching-focused collection of hand-rolled JS data structures. Each file is meant to make underlying machinery visible — not to replace nativeMap/Set/Arrayin production.11 structures (with matching Vitest test files in
data-structure/tests/):Stack— array-backed LIFOQueue— linked-list backed FIFO (avoidsArray.shiftO(n))LinkedList— singly linkedDoublyLinkedList— sentinel-node design, O(1)removeNode(ref)(the LRU cache primitive)HashMap— separate chaining + djb2 hash + load-factor rehashMaxPriorityQueue(already existed; bug-fixed — see below)MinPriorityQueue— mirror of Max with comparator flippedTrie— prefix tree,Map-of-children for UnicodeBST— unbalanced; insert/search/delete (3-case)/iterative in-orderGraph— adjacency list, undirected + directed, BFS/DFS iterativeUnionFind— path compression + union by rank, returns merge statusPlus
data-structure/README.mdwith when-to-use table and complexity cheat sheet.Why this shape
#privateFieldseverywhere. True encapsulation makes invariants enforceable and reads cleaner than_underscoreconventions.Bug fix in existing
MaxPriorityQueue.jsConstructor accepted
{ valueOf }as the priority extractor.valueOfis inherited fromObject.prototype— when destructuring{ valueOf = (x) => x } = {}, the empty-object default still resolvesvalueOfvia the prototype chain, picking upObject.prototype.valueOf(which returns the receiver). Result:priority(i)returned the heap instance itself, all comparisons evaluated to>=, and the heap silently degenerated into FIFO order onenqueue.Renamed the option to
priorityOfin both Max and Min PQ. JSDoc now warns against the trap. Caught by the new tests; would not have surfaced without them.Test plan
npm run test -- data-structure— 73/73 passing across 11 filesvalueOfbug confirmed and resolved🤖 Generated with Claude Code